package com.crm.web.controller;

import com.crm.entity.Lost;
import com.crm.service.LostService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class LostController {
    @Resource
    private LostService lostService;

    @RequestMapping(value = "/lost/list")
    public String lostlist(Model model, String lstCustName, String lstCustManagerName, @RequestParam(required = false, defaultValue = "1") int pageIndex) {
        Sort sort = Sort.by(Sort.Direction.ASC, "lstId");
        //控制分页的辅助类，可以设置页码(从0开始！！！)、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(pageIndex - 1, 5, sort);
        Page<Lost> lostPage = lostService.findLost(lstCustName, lstCustManagerName, pageable);
        model.addAttribute("rolePage", lostPage);
        model.addAttribute("lstCustName", lstCustName);
        model.addAttribute("lstCustManagerName", lstCustManagerName);
        List<Lost> roles = lostService.findAllLost();
        model.addAttribute("chance", roles);
        return "lost/list";
    }

    @RequestMapping(value = "/lostc/edit")
    public String edit(Integer lstId, Model model) {
        Lost losts = lostService.getLost(lstId);
        model.addAttribute("losts", losts);
        return "lost/edit";
    }

    @RequestMapping(value = "/lost/save")
    public String save(Lost lost) {
        lostService.saveLost(lost);
        return "redirect:/lost/list";
    }
}
